define(function () {

    var self;
    var CHARACTERISTICS = [];
    var others = {
        inx: 0
    };

    var xdcx = app.Module.extend({

        init: function () {
            self = this;
            self.mData = {
                orderSet: {}
            };
            self.searchData = {
                keyword: ''
            };
            self._super({
                _tag: 'p_xdcx', // key, 对象放进pageModule
                _sliderId: 'slider', // tabs
                _refreshAutoInit: true //自动加载刷新控件
            }, win);
        },
        update: function (d) { //批量修改状态
            var items = self._getRefreshObj().data;
            var list = [];
            for (var i = 0; i < items.length; i++) {
                if (items[i].checked == true) {
                    list.push(items[i]);
                }
            }
            var msg1 = "";
            var msg2 = "";
            var msg3;
            if (d == 'Y') {
                msg1 = "请选择需要提交的询单";
                msg2 = "批量提交成功";
                msg3 = "是否批量提交?";
            } else if (d == 'N') {
                msg1 = "请选择需要取消提交的询单";
                msg2 = "批量取消成功";
                msg3 = "是否批量取消?";
            } else if (d == 'X') {
                msg1 = "请选择需要作废的询单";
                msg2 = "批量作废成功";
                msg3 = "是否批量作废?";
            }
            if (list.length == 0) {
                app.toast(msg1);
                return;
            }
            var _data = {
                // GWCMACTI: d,
                // GWCMENT: list.GWCMENT,
                _list: []
            }
            for (var i = 0; i < list.length; i++) {
                var CHARACTERISTICS = [];
                var details = list[i].details;
                for (var j = 0; j < details.length; j++) {
                    CHARACTERISTICS.push({
                        GWCMACTI: d,
                        GWCMENT: list[i].GWCMENT,
                        GWCMDOCNO: list[i].GWCMDOCNO,
                        GWCMSEQ: details[j].GWCMSEQ
                    })

                    _data._list.push({
                        // GWCMACTI: d,
                        // GWCMENT: list[i].GWCMENT,
                        // GWCMDOCNO: list[i].GWCMDOCNO,
                        CHARACTERISTICS: CHARACTERISTICS
                    })
                }
            }
            app.ajax({
                xurl: "api/wx/gwat110/update",
                loading: true,
                confirm: {
                    msg: msg3
                },
                xdata: {
                    obj: app.jsonString(_data)
                },
                cb: function (result) {
                    if (result.ok) {
                        app.toast(msg2);
                        for (var i = 0, flag = true; i < items.length; flag ? i++ : i) {
                            if (items[i].checked == true) {
                                items.splice(i, 1);
                                flag = false;
                            } else {
                                flag = true;
                            }
                        }
                        setTimeout(function () {
                            self._refresh();
                        }, 300);
                    } else {
                        app.toast(result.data.appmsg);
                    }

                }
            })
        },
        _initRefresh: function () { // 实例化刷新组件
            if (self._refreshIds.length == 0) {
                self._refreshIds = ['scrollN', 'scrollY'];
                app.extend(self._refreshVueOpts, {
                    updated: function () {
                        mui('.mui-scroll-wrapper .child').scroll({
                            deceleration: 0.0005,
                            scrollY: false, //是否竖向滚动
                            scrollX: true, //是否横向滚动
                        });
                    }
                });
            }
            this._super(win);
        },
        _refreshCb: function (goods) {// 刷新组件回调函授
            var _super = this._super;
            app.ajax({
                xurl: "api/wx/gwat110/getGwcmDatas",
                xdata: {
                    page: goods.page+1,
                    limit: goods.limit,
                    GWCMACTI: goods.type.replace('scroll', ''),//注意页面上 不是1 2 3 是 Y N
                    keyword: self.searchData.keyword,//输入的查询条件 ：产品编号、意向客户姓名
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data;
                        _super({
                            obj: goods,
                            data: data.data.addMap({
                                checked: false
                            }),
                            nodata: self._refreshIndex == 0 ? '无未提交询单...' : (self._refreshIndex == 1 ? '无已提交询单...' : '无询单...')
                        });
                    }
                }
            });
        },
        _slide: function (i) {
            others.inx = i;
        },
        _ready: function () {
            Vue.component('xdcx-listitem', {
                template: app.getByid(doc, 'item-template'),
                props: {
                    model: Array
                },
                methods: {
                    showDetail: function (item) { //跳转到进度查询页面
                        app.openPage({
                            src: "xdcx/xdcx-info.html",
                            argsKey: 'xdcxinfo_page',
                            args: item
                        });
                    },
                    update: function (item, d) {
                        var msg = "", msg1;
                        if (d == 'Y') {
                            msg = "提交成功";
                            msg1 = '是否提交订单？';
                        } else if (d == 'N') {
                            msg = "取消成功";
                            msg1 = '是否取消订单？';
                        } else if (d == 'X') {
                            msg = "作废成功";
                            msg1 = '是否作废订单？';
                        }

                        var _data = {
                            CHARACTERISTICS: []
                        };

                        var details = item.details;
                        for (var i = 0; i < details.length; i++) {
                            _data.CHARACTERISTICS.push({
                                GWCMACTI: d,
                                GWCMENT: item.GWCMENT,
                                GWCMDOCNO: item.GWCMDOCNO,
                                GWCMSEQ: details[i].GWCMSEQ
                            })
                        }
                        app.ajax({
                            xurl: "api/wx/gwat110/updateData",
                            loading: true,
                            confirm: {
                                msg: msg1
                            },
                            xdata: {
                                obj: app.jsonString(_data)
                            },
                            cb: function (result) {
                                if (result.ok) {
                                    app.alert({
                                        msg: msg,
                                        ok: function () {
                                            setTimeout(function () {
                                                self._refresh();
                                            }, 300);
                                        }
                                    });
                                } else {
                                    app.toast(result.data.appmsg);
                                }

                            }
                        })
                    }
                }
            });

            new Vue({
                el: doc.getElementById('footdiv'),
                data: others
            });
        },
        _search:function (data) {
            self.searchData.keyword = data.keyword.trim();
            self._refresh();
        },
        _lazy: function () {
            this._super();

            $('#allCheck').click(function () { //全选
                var items = self._getRefreshObj().data;
                if ($('#allCheck').is(':checked')) {
                    $('#pullRefresh input[type=checkbox]').prop('checked', true);
                    for (var i = 0; i < items.length; i++) {
                        items[i].checked = true;
                    }
                } else {
                    $('#pullRefresh input[type=checkbox]').prop('checked', false);
                    for (var i = 0; i < items.length; i++) {
                        items[i].checked = false;
                    }
                }
            });
            $('#btn_N').on('tap', function () {
                self.update("N");
            });
            $('#btn_Y').on('tap', function () {
                self.update("Y");
            });
            $('#btn_X').on('tap', function () {
                self.update("X");
            });
        }
    });

    return new xdcx();

});